<?php

class News_m extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }

    function get_news($per_page, $off_set) {
        $this->db->select('news.*,cat_news.name, users.username');
        $this->db->from('news');
        $this->db->from('users');
        $this->db->join('cat_news', 'news.catid=cat_news.id', 'left');
        $this->db->where("users.id = news.user_post");
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_news_by_category($category_id, $per_page, $off_set) {
        $this->db->select('news.*,cat_news.name, users.username');
        $this->db->from('news');
        $this->db->from('users');
        $this->db->join('cat_news', 'news.catid=cat_news.id', 'left');
        $this->db->where("users.id = news.user_post");
        $this->db->where("cat_news.id", $category_id);
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_category_name_of_news($category_id) {
        $this->db->select('cat_news.name');
        $this->db->from('cat_news');
        $this->db->where("cat_news.id", $category_id);
             
        $query = $this->db->get();
        return $query->row();
    }
    
    function get_category_id_by_news($news_id) {
        $this->db->select('news.catid');
        $this->db->from('news');
        $this->db->where("news.id", $news_id);
             
        $query = $this->db->get();
        return $query->row();
    }

    function get_detail_news($id) {
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->join('cat_news', 'news.catid=cat_news.id');
        $this->db->where(array('news.id' => $id));
        $query = $this->db->get();
        if($query->num_rows()<1){
            return FALSE;
        }else{
            return $query;
        }
        
     
    }

    function count_news() {
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->join('cat_news', 'news.catid=cat_news.id');
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function count_news_by_category($category_id) {
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->join('cat_news', 'news.catid=cat_news.id');
        $this->db->where('cat_news.id', $category_id);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->num_rows();
    }

    function update_news($data, $id) {
        return $this->db->update('news', $data, array('id' => $id));
    }

    function add_news($data) {
        return $this->db->insert('news', $data);
    }

    function delete_news($id) {
        return $this->db->delete('news', array('id' => $id));
    }

    function delete_multi_news($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('news');
        return $query;
    }

    function get_cat_limit($per_page, $off_set) {
        $this->db->select();
        $this->db->from('cat_news');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get();
        if($query->num_rows()>0){
        	 return $query->result_array();	
        }else{
        	return array();
        }
       
    }

    function add_cat($data) {
        return $this->db->insert('cat_news', $data);
    }

    function update_cat($data, $id) {
        return $this->db->update('cat_news', $data, array('id' => $id));
    }

    function get_detail_cat($id) {
        $query = $this->db->get_where('cat_news', array('id' => $id));
        $result = $query->row_array();
        if(count($result)<1){
            return FALSE;
        }else{
            return $result;
        }
    }

    function delete_cat($id) {
        $this->db->delete('news', array('catid' => $id));
        return $this->db->delete('cat_news', array('id' => $id));
    }

    function delete_multi_cat($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('cat_news');
        return $query;
    }
    
    function get_news_home(){
        $this->db->select();
        $this->db->from('news');
        $this->db->limit(5);
        $this->db->where('available', 1);
        $this->db->order_by('posted', 'desc');
        $query = $this->db->get();
        $result = $query -> result_array();
        if(count($result)<1){
            return FALSE;
        }else{
            return $result;
        }
    }
    
    function get_available_news($per_page, $off_set){
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->where('news.available', 1);
        $this->db->join('cat_news', 'news.catid=cat_news.id', 'left');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        
        return $query->result_array();
    }
    
    function get_available_news_by_category($id, $per_page = null, $off_set = null){
        $this->db->select('news.*,cat_news.name, cat_news.id AS cat_news_id');
        $this->db->from('news');
        $this->db->where('news.available', 1);
        $this->db->where('cat_news.id', $id);
        $this->db->join('cat_news', 'news.catid=cat_news.id', 'left');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        if($query->num_rows()<1){
            return FALSE;
        }
        return $query->result_array();   
    }
    
    function count_availble_news_by_category($id){
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->where('news.available', 1);
        $this->db->where('cat_news.id', $id);
        $this->db->join('cat_news', 'news.catid=cat_news.id');
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->num_rows();    
    }
    
	function count_news_category(){
        $this->db->select('cat_news.*');
        $this->db->from('cat_news');
        $query = $this->db->get();
        return $query->num_rows();    
    }
    
    function count_avaiable_news(){
        $this->db->select('news.*,cat_news.name');
        $this->db->from('news');
        $this->db->where('news.available', 1);
        $this->db->join('cat_news', 'news.catid=cat_news.id');
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function get_news_content($id){
        $this->db->select('news.*, users.id as user_id, users.username, users.email');
        $this->db->from('news');
        $this->db->join('users', "news.user_post = users.id");
        $this->db->where('news.id', $id);
        $this->db->order_by("id", "ASC");
        $this->db->limit(1);
        $query = $this->db->get();
        if($query -> num_rows()<1){
            return FALSE;
        }else{
            return $query->row();
        }
    }
    
    function get_categories_home(){
             
        $query =  "SELECT * FROM cat_news WHERE EXISTS
            (SELECT * FROM news WHERE news.catid = cat_news.id)";
            $result = $this->db->query($query);
            return $result->result_array();
    }
    
    function get_news_of_category_limit($id, $per_page, $off_set){
            $this->db->select('news.*,cat_news.name');
            $this->db->from('news');
            $this->db->where('news.available', 1);
            $this->db->join('cat_news', 'news.catid=cat_news.id', 'left');
            $this->db->where('cat_news.id', $id);
            $this->db->limit($per_page, $off_set);
            $this->db->order_by('news.id', 'DESC');
            $query = $this->db->get();

            return $query->result_array();
    }
    
    function count_news_by_category_id($id){
            $this->db->select('news.*,cat_news.name');
            $this->db->from('news');
            $this->db->where('news.available', 1);
            $this->db->join('cat_news', 'news.catid=cat_news.id');
            $this->db->where('cat_news.id', $id);
            $this->db->order_by('news.id', 'DESC');
            $query = $this->db->get();
            return $query->num_rows();
    }
    
    function get_current_views($id, $table){
        $query = $this->db->select("total_views")
                          ->from($table)
                          ->where("id", $id)
                          ->get();
        return $query->row();
                
    }
    
    function update_total_views($id, $data, $table){
        $query = $this->db->update($table, $data, array("id" => $id));
        return $query;
    }
    
 	function count_comment_number($id){
        $query = $this->db->select("news_id")
                          ->from("news_comment")
                          ->where("id", $id)
                          ->get();
        return $query->num_rows();
                
    }
   
   
    
    function get_cat_name($id){
    		$this->db->select('cat_news.name');
            $this->db->from('cat_news');
            $this->db->where('cat_news.id', $id);
            $query = $this->db->get();
            return $query->row();
    }
    
    function get_news_comment($id){
    		$this->db->select();
            $this->db->from('news_comment');
            $this->db->where('news_comment.news_id', $id);
            $query = $this->db->get();
            return $query->result_array();
    }
    
  /*  function get_news_details($id){
    	   $query = $this->db->select("news.*, users.id as user_id, users.username")
                          ->from("news")
                          ->from("users")
                          ->join("news_user", "news.id = news_user.news_id")
                          ->where("news.id", $id)
                          ->where("users.id = news_.user_id")
                          ->get();
        	return $query->row();
    }*/
    
    function add_comment($data){
    	$query = $this->db->insert("news_comment", $data);
    	return $query;
    }
    
 	function update_comment($id, $data){
    	$query = $this->db->update("news_comment", $data, array("news_id"=>$id));
    	return $query;
    }
    
    function add_home_news($data){
    	$query = $this->db->select('news_id')
    					  ->from('home_news')
    					  ->where('id', 1)
    					  ->limit(1)
    					  ->get();
    	if($query -> num_rows()<= 0){
    		$this->db->insert("home_news", array("id"=>1));
    		return $this->db->update("home_news", $data, array("id"=>1));
    	}else{
    		return $this->db->update("home_news", $data, array("id"=>1));
    	}
    }
    
    function get_home_news(){
    	    $this->db->select('news.*,home_news.id as home_news_id, home_news.date_posted');
            $this->db->from('home_news');
            $this->db->where('home_news.id', 1);
            $this->db->join('news', 'news.id=home_news.news_id');
            $this->db->limit(1);
            $query = $this->db->get();
            return $query->row();	
    }
    
    

}

?>
